home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / scripts / plot / hist.m < prev    next >
Text File  |  1996-07-15  |  3KB  |  101 lines

  1. ## Copyright (C) 1996 John W. Eaton
  2. ##
  3. ## This file is part of Octave.
  4. ##
  5. ## Octave is free software; you can redistribute it and/or modify it
  6. ## under the terms of the GNU General Public License as published by
  7. ## the Free Software Foundation; either version 2, or (at your option)
  8. ## any later version.
  9. ##
  10. ## Octave is distributed in the hope that it will be useful, but
  11. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. ## General Public License for more details.
  14. ##
  15. ## You should have received a copy of the GNU General Public License
  16. ## along with Octave; see the file COPYING.  If not, write to the Free
  17. ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  18. ## 02111-1307, USA.
  19.  
  20. ## usage: [NN, XX] = hist (Y, X)  or  hist (Y, X)
  21. ##
  22. ## Produce histogram counts or plots.
  23. ##
  24. ## With one vector input argument, plot a histogram of the values with
  25. ## 10 bins.  The range of the histogram bins is determined by the range
  26. ## of the data.
  27. ##
  28. ## Given a second scalar argument, use that as the number of bins.
  29. ##
  30. ## Given a second vector argument, use that as the centers of the bins,
  31. ## with the width of the bins determened from the adjacent values in
  32. ## the vector.
  33. ##
  34. ## Extreme values are lumped in the first and last bins.
  35. ##
  36. ## With two output arguments, produce the values NN and XX such that
  37. ## bar (XX, NN) will plot the histogram.
  38. ##
  39. ## See also: bar
  40.  
  41. ## Author: jwe
  42.  
  43. function [nn, xx] = hist (y, x)
  44.  
  45.   if (nargin < 1 || nargin > 2)
  46.     usage ("[nn, xx] = hist (y, x)");
  47.   endif
  48.  
  49.   if (is_vector (y))
  50.     max_val = max (y);
  51.     min_val = min (y);
  52.   else
  53.     error ("hist: first argument must be a vector");
  54.   endif
  55.  
  56.   if (nargin == 1)
  57.     n = 10;
  58.     delta = (max_val - min_val) / n / 2;
  59.     x = linspace (min_val+delta, max_val-delta, n);
  60.     cutoff = x + delta;
  61.   elseif (nargin == 2)
  62.     if (is_scalar (x))
  63.       n = x;
  64.       if (n <= 0)
  65.         error ("hist: number of bins must be positive");
  66.       endif
  67.       delta = (max_val - min_val) / n / 2;
  68.       x = linspace (min_val+delta, max_val-delta, n);
  69.       cutoff = x + delta;
  70.     elseif (is_vector (x))
  71.       tmp = sort (x);
  72.       if (any (tmp != x))
  73.         warning ("hist: bin values not sorted on input");
  74.         x = tmp;
  75.       endif
  76.       n = length (x);
  77.       cutoff = zeros (1, n-1);
  78.       for i = 1:n-1
  79.         cutoff (i) = (x (i) + x (i+1)) / 2;
  80.       endfor
  81.     else
  82.       error ("hist: second argument must be a scalar or a vector");
  83.     endif
  84.   endif
  85.  
  86.   freq = zeros (1, n);
  87.   freq (1) = sum (y < cutoff (1));
  88.   for i = 2:n-1
  89.     freq (i) = sum (y >= cutoff (i-1) & y < cutoff (i));
  90.   endfor
  91.   freq (n) = sum (y >= cutoff (n-1));
  92.  
  93.   if (nargout == 2)
  94.     nn = freq;
  95.     xx = x;
  96.   else
  97.     bar (x, freq);
  98.   endif
  99.  
  100. endfunction
  101.